package conput;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.concurrent.CountDownLatch;

public class Main {

    public static void main(String[] args) {
        final int operations = 1000000;
        final int trials = 8;
        for (Server server : new Server[]{new RWLALServer(), new RWLHMServer(), new CHMServer()}) {
            String contents = "";
            for (final int clients : new int[]{1, 2, 4, 8, 16}) {
                double average = 0;
                for (int trial = 0; trial < trials; trial++) {
                    final CountDownLatch startSignal = new CountDownLatch(1);
                    final CountDownLatch doneSignal = new CountDownLatch(clients);
                    for (int i = 0; i < clients; i++)
                        new Client(operations / clients, startSignal, doneSignal, server).start();
                    long start = System.nanoTime();
                    startSignal.countDown();
                    try {
                        doneSignal.await();
                    } catch (InterruptedException e) {
                        System.err.println(e);
                    } finally {
                        long end = System.nanoTime();
                        average +=  (end - start) / (double) operations;

                    }
                }
                contents += "[" + clients + ", " + average / trials + "], ";
            }
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(args[0] + server.getClass().getName().split("conput.")[1].split("Server")[0], true));
                out.write(contents);
                out.close();
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }
}
